﻿using Newtonsoft.Json.Linq;
using System.Text.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Butcher.winform.Dto;
using Traceability.Commont.Domains;

namespace slaughters
{
    public partial class SlaughterList : Form
    {
        public SlaughterList()
        {
            InitializeComponent();
            slaufhterListess();

        }
        SlaughterListSize size = new SlaughterListSize();
        public int pageIndex = 1;//页码
        public int pageSize = 2;//页容量
        public int count = 0;//总条数
        public int page = 0;//总页数

        //企业显示
        private void SlaughterList_Load(object sender, EventArgs e)
        {
            

        }
        private void slaufhterListess()
        {
            GetEnter infoEnter = new GetEnter();
            infoEnter.EnterpriseName = "";
            infoEnter.pageIndex = pageIndex;
            infoEnter.pageSize = pageSize;
            string url = @"http://localhost:5229/api/Enterprise/GetEnterprises";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<GetEnter>(infoEnter));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["lists"].Value<JArray>();
            var counts = info["totalCount"];
            var pages = info["pageCount"];
            count = Convert.ToInt32(counts);
            page = Convert.ToInt32(pages);
            List<Enterprises> enterprises = aaa.ToObject<List<Enterprises>>();
            SlaufhterDto.enterprise = enterprises;
            dgv_slaufhterList.DataSource = SlaufhterDto.enterprise;
            textBox1.Text= pageSize.ToString();
            textBox2.Text= pageIndex.ToString();
        }

        private void dgv_slaufhterList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
      
        public class SlaughterListSize 
        {
            public int pageIndex { get; set; }
            public int pageSize {  get; set; }


        }


        private void button2_Click(object sender, EventArgs e)
        {

            if (pageIndex == 1)
            {
                MessageBox.Show("已经是首页了");

            }
            else
            {
                pageIndex -= 1;
            }
            slaufhterListess();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            slaufhterListess();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pageIndex = page;
            slaufhterListess();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            if (pageIndex == page)
            {
                MessageBox.Show("已经是尾页了");

            }
            else
            {
                pageIndex += 1;
            }
            slaufhterListess();
        }
    }
}
